home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / xlib04.zip / XFILEIO.ASM < prev    next >
Assembly Source File  |  1992-11-12  |  8KB  |  308 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XFILEIO
  3. ;
  4. ; Sequential binary file I/O functions
  5. ;
  6. ; Some functions based on a r.g.p post by Joshua Jensen
  7. ;
  8. ; Compile with Tasm.
  9. ; C callable.
  10. ;
  11. ;
  12. ; ****** XLIB - Mode X graphics library                ****************
  13. ; ******                                               ****************
  14. ; ****** Written By Themie Gouthas                     ****************
  15. ;
  16. ; egg@dstos3.dsto.gov.au
  17. ; teg@bart.dsto.gov.au
  18. ;-----------------------------------------------------------------------
  19. COMMENT $
  20.  
  21.  
  22. $
  23.  
  24. LOCALS
  25. .286
  26.  
  27. include model.inc
  28. include xfileio.inc
  29.  
  30.     .code
  31.  
  32. PUSH_DS macro
  33.     IFNDEF s
  34.     push  ds
  35.     ENDIF
  36.     endm
  37.  
  38. POP_DS macro
  39.     IFNDEF s
  40.     pop   ds
  41.     ENDIF
  42.     endm
  43.  
  44. LDS_M macro arg1,arg2
  45.     IFNDEF s
  46.     lds &arg1&,&arg2&
  47.     ELSE
  48.     mov &arg1&,word ptr &arg2&
  49.     ENDIF
  50.     endm
  51.  
  52.  
  53.  
  54. ;****************************************************************
  55. ;
  56. ; name: f_open
  57. ;
  58. ; C Prototype:
  59. ;
  60. ;     extern int f_open(char * filename, char access)
  61. ;
  62. ; Opens a file according to the access char:
  63. ;
  64. ;   0 = read only   - If doesnt exist return error
  65. ;   1 = write only  - If doesnt exist create it otherwise clear it
  66. ;   2 = read/write  - If doesnt exist create it
  67. ;
  68. ; Returns the file handle on success, -1 on failure
  69. ;
  70. ;
  71. proc _f_open
  72. IFNDEF s
  73.   ARG   filename:dword,access:byte
  74. ELSE
  75.   ARG   filename:word,access:byte
  76. ENDIF
  77.     push  bp             ; Preserve caller's stack frame
  78.     mov   bp,sp
  79.     PUSH_DS
  80.     LDS_M dx,[filename]  ; point DS:DX to file name string
  81.         cmp   [access],1
  82.     je    @@creat
  83.  
  84.     mov   ah,3dh         ; select "open file" DOS service
  85.     mov   al,[access]    ; select access type code
  86.     int   21h            ; call DOS service
  87.     jnb   @@Done         ; If carry flag set we have failed
  88.  
  89.  
  90.     cmp   [access],2
  91.     jne   @@error
  92. @@creat:
  93.     mov   ah,3ch         ; select "creat file" DOS service
  94.     mov   cx,0
  95.     int   21h            ; call DOS service
  96.     jnb   @@Done         ; If carry flag set we have failed
  97. @@error:
  98.     mov   ax,-1          ;  indicate failure
  99. @@Done:                      ; otherwise return file handle
  100.     POP_DS
  101.     pop  bp              ;restore caller's stack frame
  102.     ret
  103. _f_open endp
  104.  
  105.  
  106. ;****************************************************************
  107. ;
  108. ; name: f_close
  109. ;
  110. ; C Prototype:
  111. ;
  112. ;     extern int f_close(int handle)
  113. ;
  114. ; Closes the file associated with the specified handle
  115. ;
  116. ; Returns 0 on success, -1 on failure
  117. ;
  118. proc _f_close
  119. ARG   handle:word
  120.     push bp             ; Preserve caller's stack frame
  121.     mov  bp,sp
  122.  
  123.     mov  ah,3eh         ; select  "close file handle" DOS service
  124.     mov  bx,[handle]    ; select handle of file to close
  125.     int  21h            ; call DOS service
  126.     jnb  @@Fix          ; failed if carry flag set
  127.     mov  ax,-1          ;  return error
  128.     jmp  short @@Done
  129. @@Fix:                      ; otherwise
  130.     xor  ax,ax          ;  return 0
  131. @@Done:
  132.     pop  bp             ;restore caller's stack frame
  133.     ret
  134. _f_close endp
  135.  
  136.  
  137. ;****************************************************************
  138. ;
  139. ; name: f_read
  140. ;
  141. ; C Prototype:
  142. ;
  143. ;     extern int f_read(int handle, char far * buffer, int count)
  144. ;
  145. ; Reads a block of count bytes from the file specified by the handle
  146. ; into the buffer
  147. ;
  148. ; Returns count on success, -1 on failure
  149. ;
  150. proc _f_read
  151. ARG   handle:word,buffer:dword,count:word
  152.     push bp             ; Preserve caller's stack frame
  153.     mov  bp,sp
  154.     push ds
  155.  
  156.     mov  ah,3fh         ; select "read from file or device" DOS service
  157.     mov  bx,[handle]    ; select handle of file to close
  158.     mov  cx,[count]
  159.     lds  dx,[buffer]
  160.     int  21h            ; call DOS service
  161.     jnb  @@Fix          ; failed if carry flag set
  162.     mov  ax,-1          ;  return error
  163.     jmp  short @@Done
  164. @@Fix:                      ; otherwise
  165.     xor  ax,ax          ;  return 0
  166. @@Done:
  167.     pop  ds
  168.     pop  bp             ;restore caller's stack frame
  169.     ret
  170. _f_read endp
  171.  
  172. ;****************************************************************
  173. ;
  174. ; name: f_write
  175. ;
  176. ; C Prototype:
  177. ;
  178. ;     extern int f_write(int handle, char far * buffer, int count)
  179. ;
  180. ; Writes a block of count bytes to the file specified by the handle
  181. ; from the buffer
  182. ;
  183. ; Returns count on success, -1 on failure
  184. ;
  185. proc _f_write
  186. ARG   handle:word,buffer:dword,count:word
  187.     push bp             ; Preserve caller's stack frame
  188.     mov  bp,sp
  189.     push ds
  190.  
  191.     mov  ah,40h         ; select "write to file or device" DOS service
  192.     mov  bx,[handle]    ; select handle of file to write
  193.     mov  cx,[count]
  194.     lds  dx,[buffer]
  195.     int  21h            ; call DOS service
  196.     jnb  @@Done         ; has the function failed ?
  197.     mov  ax,-1          ;  yes, return error
  198.     jmp  short @@Done
  199. @@Done:                     ; otherwise return bytes written
  200.     pop  ds
  201.     pop  bp             ; restore caller's stack frame
  202.     ret
  203. _f_write endp
  204.  
  205. ;****************************************************************
  206. ;
  207. ; name: f_seek
  208. ;
  209. ; C Prototype:
  210. ;
  211. ;   extern long int f_seek(int handle, long int position, char method_code)
  212. ;
  213. ; Moves the file pointer according to the position and method code
  214. ;
  215. ; Returns file pointer position on success, -1 on failure
  216. ;
  217. proc _f_seek
  218. ARG   handle:word,position:dword,method_code:byte
  219.     push bp             ; Preserve caller's stack frame
  220.     mov  bp,sp
  221.  
  222.     mov  ah,42h         ; select "move file pointer" DOS service
  223.     mov  bx,[handle]    ; select handle of file to close
  224.     mov  al,[method_code]
  225.     mov  cx,word ptr [position+2]
  226.     mov  dx,word ptr [position]
  227.     int  21h            ; call DOS service
  228.     jnb  @@Done         ; has the function failed ?
  229.     mov  ax,-1          ;  yes, return error
  230.     mov  dx,-1          ;
  231.     jmp  short @@Done
  232. @@Done:                     ; otherwise return bytes written
  233.     pop  bp             ; restore caller's stack frame
  234.     ret
  235. _f_seek endp
  236.  
  237.  
  238. ;****************************************************************
  239. ;
  240. ; name: f_filelength
  241. ;
  242. ; C Prototype:
  243. ;
  244. ;   extern long int f_filelength(int handle)
  245. ;
  246. ; Returns the length of the file associated with the specified handle
  247. ;
  248. ; Returns file length on success, -1 on failure
  249. ;
  250. proc _f_filelength
  251. ARG     handle:word
  252. LOCAL   low:word,high:word=LocalStk
  253.     push bp             ; Preserve caller's stack frame
  254.     mov  bp,sp
  255.     sub  sp,LocalStk
  256.  
  257.     ; Get ptr's current location in file and save it
  258.  
  259.     mov  ah,42h         ; select "move file pointer" DOS service
  260.     mov  al,1           ; select "from current location" method
  261.     mov  bx,[handle]    ; select handle of file to close
  262.     xor  cx,cx
  263.     xor  dx,dx
  264.     int  21h
  265.     jb   @@Error
  266.     mov  [low],ax
  267.     mov  [high],dx
  268.  
  269.     ; Get ptr's value at end of file
  270.  
  271.     mov  ah,42h         ; select "move file pointer" DOS service
  272.     mov  al,2           ; select "from end of file" method
  273.         mov  bx,[handle]    ; select handle of file to close
  274.     xor  cx,cx
  275.     xor  dx,dx
  276.     int  21h
  277.     jb   @@Error
  278.  
  279.     ; Save the results while returning pointer to its previous location
  280.  
  281.     push ax
  282.     push dx
  283.  
  284.         mov  ah,42h         ; select "move file pointer" DOS service
  285.     mov  al,0           ; select "from start of file" method
  286.         mov  bx,[handle]    ; select handle of file to close
  287.     mov  cx,[high]
  288.     mov  dx,[low]
  289.     int  21h
  290.  
  291.     ; restore resultant length
  292.  
  293.     pop  dx
  294.     pop  ax
  295.     jnb   @@Done        ; Was the operation a success ?
  296. @@Error:
  297.     mov  ax,-1          ;  no, return error
  298.     mov  dx,-1          ;
  299. @@Done:                     ; otherwise return bytes written
  300.     mov  sp,bp
  301.     pop  bp             ; restore caller's stack frame
  302.     ret
  303. _f_filelength endp
  304.  
  305.  
  306.     end
  307.  
  308.